# coding=utf-8
from bs4 import BeautifulSoup
import time
import re
from  baidu.models import Question,Answer
import requests

class URLResponse:
    headers = {
        'Host':"zhidao.baidu.com",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Connection": "keep-alive",
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36",
        "Content-Type" : "application/x-www-form-urlencoded",
        "Upgrade-Insecure-Requests" : "1"
    }
    def __init__(self, url):
        self.url = url
        
    def getHtml(self, method = 'get', data = {}):
        if method == 'get':
            return requests.get(self.url, headers = self.headers, timeout = 10)



class Baidu:
    
    def zhidao(self, url ,tag = ""):
        """
        公司url 职位url提取入库
        """
        all_the_text = URLResponse(url).getHtml()
        soup = BeautifulSoup(all_the_text.content.decode('gbk'))
        cUrlsCompiles = [
            re.compile("http://zhidao.baidu.com/question/([0-9]+\.html$)"),
        ]
        objs = soup.find('div',class_="list-inner").find('div',class_="list").find_all(href = cUrlsCompiles)
        for o in objs:
            answer_arr = []
            try:
                answer = URLResponse(o['href']).getHtml()
                print(o['href'])
                answersoup = BeautifulSoup(answer.content.decode('gbk'))
                title = answersoup.find("span", class_="ask-title")
                content = answersoup.find("div", class_="line mt-5 q-content")

                best_answer = answersoup.find("pre", class_="best-text mb-10")
                best_answer and answer_arr.append({'answer': best_answer.text, "best" : 1})

                other_answer = answersoup.find_all("div", class_="answer-text line")
                for a in other_answer:
                    answer_arr.append({'answer': a.text, "best" : 0})

                question = Question.objects.create(**{
                    "title" : title and title.text or "",
                    "desc" : content and content.text or "",
                    "tag" : tag
                })
                if question:
                    for ans in answer_arr :
                        Answer.objects.create(**{
                            "question" : question.id,
                            "answer" : ans.get('answer',''),
                            "isbest" : ans.get('best'),
                        })
                
            except:
                pass
            
            time.sleep(2)
            
    
    def _filter(self, strs):
        if strs:
            return strs.strip()
        return ""
    
    

